<template>
	<view>
		<u-tabbar :value="index" @change="changepage" :fixed="true" :placeholder="false" activeColor="#3ad3bc"
			:safeAreaInsetBottom="true">

			<u-tabbar-item v-for="(item,index) in pageList" :key="item.text" :text="item.text"
				:badge="index==2?number:0">
				<image class="tabicon" slot="active-icon" :src="item.inActionIcon"></image>
				<image class="tabicon" slot="inactive-icon" :src="item.actionIcon"></image>
			</u-tabbar-item>

		</u-tabbar>

	</view>
</template>

<script>
	import {
		genTestUserSig
	} from '@/TUIKit/debug/GenerateTestUserSig';
	import TencentCloudChat from '@tencentcloud/chat';
	import {
		TUILogin
	} from '@tencentcloud/tui-core';
	// #ifdef APP-PLUS || H5
	import {
		TUIChatKit
	} from '../../TUIKit';

	TUIChatKit.init();
	// #endif

	let vueVersion = 2;
	// #ifdef VUE3
	vueVersion = 3;
	// #endif
	export default {
		data() {
			return {
				number: 0,
				pageList: [{
						text: "首页",
						pagePath: "/pages/index/index",
						actionIcon: "../../static/icon/首页.png",
						inActionIcon: "../../static/icon/首页-copy.png"

					},
					{
						text: "全部服务",
						pagePath: "/pages/AllServices/AllServices",
						actionIcon: "../../static/icon/全部服务.png",
						inActionIcon: "../../static/icon/全部服务-copy.png"
					},
					{
						text: "消息",
						pagePath: "/pages/Information/Information",
						actionIcon: "../../static/icon/信息.png",
						inActionIcon: "../../static/icon/信息-copy.png"
					},
					{
						text: "我的",
						pagePath: "/pages/My/My",
						actionIcon: "../../static/icon/我的.png",
						inActionIcon: "../../static/icon/我的-copy.png"
					}
				]
			}
		},
		computed: {
			index() {
				return this.$store.state.tabbarIndex
			}
		},
		created() {


			// console.log("xx", TUILogin.getTotalUnreadMessageCount());
			// let totalUnreadCount = TUILogin.getTotalUnreadMessageCount();
			// let totalUnreadCount2 = TUILogin.on(TencentCloudChat.EVENT.TOTAL_UNREAD_MESSAGE_COUNT_UPDATED,
			// 	onTotalUnreadMessageCountUpdated);
			// console.log('w未读', totalUnreadCount);

		},
	
		mounted() {
			const that=this
			let onTotalUnreadMessageCountUpdated = function(event) {
				that.number = event.data; // 使用 `this.number` 来更新 `number`
				    console.log('当前未读消息数:', that.number); // 输出当前未读的总数
				// this.number = event.data
				// console.log(event.data); // 当前单聊和群聊会话的未读总数
				// console.log("舒总",this.number); // 当前单聊和群聊会话的未读总数
				
			};
			uni.$chat.on(TencentCloudChat.EVENT.TOTAL_UNREAD_MESSAGE_COUNT_UPDATED, onTotalUnreadMessageCountUpdated);
		},
		methods: {



			changepage(index) {
				// console.log(index);
				// if (this.pageList[index].pagePath == "/TUIKit/components/TUIConversation/index") {
					if (uni.getStorageSync("token") && uni.getStorageSync("isReady")) {
						const user = uni.getStorageSync("userinfo")

						const aa = genTestUserSig({
							SDKAppID: 1600084628,
							secretKey: "be7e8cc168feb00b6c562b4ef8c46ccfea630bd3325d8cad87e351ddd778a165",
							userID: JSON.parse(user).username
						})
						console.log('信息', aa);
						TUILogin.login({
							SDKAppID: 1600084628,
							userID: JSON.parse(user).username,
							userSig: aa.userSig,
							useUploadPlugin: true, // If you need to send rich media messages, please set to true.
							framework: `vue${vueVersion}` // framework used vue2 / vue3
						}).then(res => {
							console.log("xiao洗", res);
							uni.setStorageSync("isReady", true)
							// uni.navigateTo({
							// 	url: "/TUIKit/components/TUIConversation/index"
							// })
						}).catch(() => {});

					} else {
						uni.showToast({
							title: "请先的登录！"
						})
						uni.reLaunch({
							url: "/pages/My/My"
						})
					}

				// } else {
					uni.switchTab({
						url: this.pageList[index].pagePath
					})
				// }

			}

		}
	}
</script>

<style lang="less">
	.tabicon {
		width: 18px;
		height: 18px;
	}
</style>